package com.qn.strategy.event;

import com.qn.strategy.ICouponDiscount;
import com.qn.strategy.eums.EnumPromotionType;
import com.qn.strategy.factory.PromotionTypeServiceStrategyFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 折扣
 *
 * @author Qning
 * @date 2021/09/09
 */
@Service
public class ZKCouponDiscount implements ICouponDiscount<Double>, InitializingBean {


    /**
     * 折扣计算
     * 1. 使用商品价格乘以折扣比例，为最后支付金额
     * 2. 保留两位小数
     * 3. 最低支付金额1元
     */
    @Override
    public BigDecimal discountAmount(Double couponInfo, BigDecimal skuPrice) {
        BigDecimal discountAmount = skuPrice.multiply(new BigDecimal(couponInfo)).setScale(2, BigDecimal.ROUND_HALF_UP);
        if (discountAmount.compareTo(BigDecimal.ZERO) < 1) {
            return BigDecimal.ONE;
        }
        return discountAmount;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        PromotionTypeServiceStrategyFactory.register(EnumPromotionType.ZK.getValue(), this);
    }
}